package com.hk.servlet;

import com.alibaba.fastjson.JSONObject;
import com.hk.model.NewsType;
import com.hk.model.ProductMenu;
import com.hk.model.vo.ResultVO;
import com.hk.service.IProductMenuService;
import com.hk.service.impl.ProductMenuServiceImpl;
import com.hk.utils.CharacterEncoding;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/24 16:27
 * @Statement: 查询所有产品类型
 */
@WebServlet("/productMenu")
public class ProductMenuServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        统一字符编码
        CharacterEncoding.setCharacterEncodingResp(req, resp, "UTF-8");
        IProductMenuService iProductMenuService = new ProductMenuServiceImpl();
        List<ProductMenu> productMenuList = iProductMenuService.findAll(-1, -1);
        ResultVO<ProductMenu> resultVO = new ResultVO<ProductMenu>();
        PrintWriter out = resp.getWriter();
        if (!productMenuList.isEmpty()) {
            resultVO.setStatus(1);
            resultVO.setMessage("success");
            resultVO.setTotal(productMenuList.size());
            resultVO.setList(productMenuList);
        } else {
            resultVO.setStatus(0);
            resultVO.setMessage("error");
            resultVO.setTotal(productMenuList.size());
            resultVO.setList(productMenuList);
        }
        String resultVOJSON = JSONObject.toJSONString(resultVO);
        out.write(resultVOJSON);
        out.flush();
        out.close();


    }


}
